Omplus.views.TableRow = Omplus.base.extend(Omplus.views.Container, {
    init:function () {
        for (var i in this.components) {
            this.components[i].render = function () {
                if (Omplus.globals.getObject(this.model))
                this.setValue(Omplus.globals.getObject(this.model).getValue());
            };
            this.components[i].omplusName = this.omplusName + "_" + this.components[i].omplusName;
            if (!this.components[i].controller){
                this.components[i].model = this.model + "." + this.components[i].model;
                this.components[i].controller = this.controller;
            }
            if (this.rowCellWidths[i]) {
                this.components[i].width = this.rowCellWidths[i];
            } else {
                this.components[i].width = (100 / this.components.length) + "%";
            }
            this.components[i].index = this.index;
        }
    },
    render:function () {
        this.getContent().attr("id", this.omplusName).addClass("tableRow");
    },
    layout:{
        omplusType:"HorizontalLayout"
    },
    omplusType:"TableRow",

    index : null
});

Omplus.views.Table = Omplus.base.extend(Omplus.views.Container, {

    render:function () {
        this.getContent().attr("id", this.omplusName).addClass("table");
    },

    omplusType:"Table",

    components:null,

    layout:{
        omplusType:"VerticalLayout"

    },

    model:null,

    rowHeight:null,

    rowCellWidths:[],

    setValue:function (newValue) {
        this.empty();
        this.components = [];
        for (var row in newValue) {
            var nrow = Omplus.base.create(Omplus.views[this.omplusProto], {
                omplusName:this.omplusName + "_row_" + row,
                model:this.generateModel(row, newValue),
                controller:this.generateController(),
                height:this.rowHeight,
                rowCellWidths:this.rowCellWidths,
                index : row
            });
            this.components.push(this.omplusName + "_row_" + row);
            this.append(this.omplusName + "_row_" + row);
        }
    },
    empty:function () {
        for (var i in this.components) {
            if (Omplus.globals.getObject(this.components[i]))
                Omplus.globals.getObject(this.components[i]).destroy();
        }
        this.clear();
    },
    generateModel:function (row, newValue) {
        return this.model + ".member." + row;
    },
    generateController:function () {
        return this.controller;
    }
});

Omplus.views.ScrollTable = Omplus.base.extend(Omplus.views.Table, {
    render:function () {
        this.getContent().addClass("scrollable");
    }
});

Omplus.views.PaginationTable = Omplus.base.extend(Omplus.views.Table, {
    init:function () {
        if (this.orientation == "horizontal") {
            this.layout = {
                omplusType:"HorizontalPaginationLayout",
                rowsPerPage:this.rowsPerPage,
                rowHeight:this.rowHeight
            }
        } else if (this.orientation == "vertical") {
            this.layout = {
                omplusType:"VerticalPaginationLayout",
                rowsPerPage:this.rowsPerPage,
                rowHeight:this.rowHeight
            }
        }
    },

    height:"100%",

    rowHeight:"50px",
    rowsPerPage:1,

    changePerPage : function(number){
        this.rowsPerPage = number;
        var list = Omplus.globals.getObject(this.model).getValue();
        this.layout.rowsPerPage = number;
        Omplus.globals.getObject(this.omplusName + ".Layout").rowsPerPage = number;
        if (list.length){
            this.setValue(list);
        }
    },

    setValue:function (newValue) {
        this.empty();
        this.components = [];
        for (var row in newValue) {
            Omplus.base.create(Omplus.views[this.omplusProto], {
                omplusName:this.omplusName + "_row_" + row,
                model:this.model + ".member." + row,
                controller:this.controller,
                height:this.rowHeight,
                rowCellWidths:this.rowCellWidths
            });
            this.components.push(this.omplusName + "_row_" + row);
        }
        this.pages = Math.ceil(newValue.length / this.layout.rowsPerPage);
        this.appendComponents();
    },

    appendComponents:function () {
        this.clear();
        if (this.hasPrivilege()) {
            if (this.components) {
                var helper = this;
                this.currentPage = 1;
                Omplus.globals.getObject(this.omplusName + ".Layout").appendPagination({
                    omplusName:this.omplusName + "_pagination",
                    pages:this.pages,
                    displayedPages:this.layout.rowsPerPage,
                    onPageClick:function (pageNumber) {
                        helper.onPageChanged(pageNumber);
                    }
                });
            }
            for (var i in this.components) {
                if (Omplus.globals.getObject(this.components[i]).hasPrivilege())
                    this.append(this.components[i]);
            }
        }
    },

    onPageChanged:function (pageNumber) {
        if (this.orientation == "horizontal") {
            var width = 100;
            var current = (this.currentPage - 1) * width * (-1);
            var change = (pageNumber - 1) * width * (-1);
            if (Math.abs(current - change)) {
                var margin;
                if (current > change) margin = "-";
                else margin = "+";
                margin += "=" + Math.abs(current - change) + "%";
                var count = Math.abs(this.currentPage - pageNumber);
                if (count > 3) {
                    count = 3;
                }
                this.getContent().find(".pages").animate({
                    "margin-left":margin
                }, 300 * count);
                this.currentPage = pageNumber;
            }
        } else if (this.orientation == "vertical") {
            var height = this.getContent().find(".page:first").height();
            var current = (this.currentPage - 1) * height * (-1);
            var change = (pageNumber - 1) * height * (-1);
            if (Math.abs(current - change)) {
                var margin;
                if (current > change) margin = "-";
                else margin = "+";
                margin += "=" + Math.abs(current - change) + "px";
                var count = Math.abs(this.currentPage - pageNumber);
                if (count > 3) {
                    count = 3;
                }
                this.getContent().find(".pages").animate({
                    "margin-top":margin
                }, 300 * count);
                this.currentPage = pageNumber;
            }
        }
    }
});

Omplus.views.DinamicTable = Omplus.base.extend(Omplus.views.PaginationTable, {
    omplusType:"DinamicTable",
    init : function(){
        this.layout = {
            omplusType:"DinamicLayout",
            rowHeight:this.rowHeight
        }
    },

    total : null,

    definition : {
        ownerOfAction : null,
        nameOfAction : null,
        list : null,
        modelSearch : null
    },

    onPageChanged : function(pageNumber){
        this.currentPage = pageNumber;
        var object = {
            start : (pageNumber - 1) * this.rowsPerPage,
            delta : this.rowsPerPage
        }
        Omplus.globals.getObject(this.definition.modelSearch).setValue(object, this.omplusName);
        Omplus.globals.getObject(this.definition.ownerOfAction).getAction(this.definition.nameOfAction).run();
    },

    setValue:function (newValue) {
        this.total = newValue.total;
        this.empty();
        this.components = [];
        for (var row in newValue[this.definition.list]) {
            Omplus.base.create(Omplus.views[this.omplusProto], {
                omplusName:this.omplusName + "_row_" + row,
                model:this.model + "." + this.definition.list + ".member." + row,
                controller:this.controller,
                height:this.rowHeight,
                rowCellWidths:this.rowCellWidths
            });
            this.components.push(this.omplusName + "_row_" + row);
        }
        this.pages = Math.ceil(newValue[this.definition.list].length / this.layout.rowsPerPage);
        this.appendComponents();
    },

    changePerPage : function(number){
        this.rowsPerPage = number;
        this.onPageChanged(1);
    },

    appendComponents:function () {
        this.clear();
        if (this.hasPrivilege()) {
            if (this.components) {
                var helper = this;
                if (!this.currentPage && this.currentPage == null)
                    this.currentPage = 1;
                Omplus.globals.getObject(this.omplusName + ".Layout").appendPagination({
                    omplusName:this.omplusName + "_dinamic",
                    pages:Math.ceil(this.total / this.rowsPerPage),
                    edges : 3,
                    currentPage : this.currentPage,
                    displayedPages:5,
                    onPageClick:function (pageNumber) {
                        helper.onPageChanged(pageNumber);
                    }
                });
            }
            for (var i in this.components) {
                if (Omplus.globals.getObject(this.components[i]).hasPrivilege())
                    this.append(this.components[i]);
            }
        }
    }


});